package com.awenlchan.musicbox;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

import java.io.IOException;

public class MusicService extends Service {
    private MediaPlayer player = null ;
    private boolean isPause = false;
    private String currentMusic = null;
    private MusicSpy spy = null;

    private String action = null ;
    private String music = null;

    public MusicService() {

    }
    private void go(){
        player = new MediaPlayer();
        Log.i("awenlchan","player创建成功");
        new Thread(){
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {  e.printStackTrace();    }
                    if(player!=null&&player.isPlaying()){
                        spy.setMax(player.getDuration());
                        spy.setProgress(player.getCurrentPosition());
                        action = spy.getAction();
                        music = spy.getMusic();
                    }
                }

            }
        }.start();

    }

    @Override
    public void onCreate() {
        super.onCreate();
        spy = new MusicSpy();
        spy.setMs(this);
        Log.i("awenlchan","Creating service-----");
        go();

    }

    @Override
    public IBinder onBind(Intent intent) {

        Log.i("awenlchan","service binding---");
        return spy;
    }

    public void play(String music){
        player.reset();
        currentMusic = music;
        try {
            player.setDataSource(music);
            player.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        player.start();




    }
    public void stopPlay(){
        player.stop();
    }
    public void pause(){
        player.pause();
    }
}
